﻿#pragma once
#ifndef CHATSOCKET_H
#define CHATSOCKET_H
#include <QTcpSocket>
#include <QHostAddress>
#include <QObject>
#include <QThread>
#include <QDateTime>
#include "MyPDU.h"
#include "conf.h"
#include <QThread>
#include <QMessageBox>
#include <QDir>
#include <QFile>
#include "json.h"
#include <QCoreApplication>
#include "chatdb.h"
#include "mainwidget.h"
#include "chatwidget.h"
#include "groupwidget.h"
class ChatWidget;
class GroupWidget;
class ChatSocket:public QObject
{
    Q_OBJECT
public:
    ~ChatSocket();
    bool intiAll();//初始化连接所有sock
    void unInit();//解初始化
    static ChatSocket* getInstance();
    void handleLoginS(Json::Value& root);
    void createFileAndWriteAcount();//创建用户文件夹并写入账号密码等
    void OnRegister(QString nickname,QString pwd);
    void sendChatData(Json::Value& root);
    QString getUserID();
private:
    void handleRecvMsg(Json::Value& root);//收到私聊消息
    void handleOffLineLog(Json::Value& root);
    void hanldeOnlineStatus(QString id,bool status);
    void handleAddFriend(Json::Value& root);
    void handleAddFriendS(Json::Value& root);
    void handleAddFriendF(Json::Value& root);
signals:
    void transMSG(MsgType type, QString msg="");
    void transSearchResult(Json::Value& root);
public slots:
    void handleChatDate();//处理服务器回复
    void sendTextMsg(QString msg,QString recv);//发送文本消息
    void handleStatuChange(QAbstractSocket::SocketState socketState);
    void onLogin(QString id, QString pwd, bool isSavePwd = false);
private:
    ChatSocket();
    QTcpSocket* m_pChatSock;//用于聊天
    QTcpSocket* m_pImgSock;//用于图片
    QTcpSocket* m_pFileSock;//用于文件
    QTcpSocket* m_pAudioSock;//用于音频
    QTcpSocket* m_pVideoSock;//用于视频
    QThread* m_pChatThread;
    QString m_userID;//记录用户账号
    QString m_pwd;//记录密码
    static ChatSocket* m_pChatSocket;
    bool m_bisInit;//是否初始化
    bool m_ConnectionStatus;
    bool m_isSavePwd;//是否保存密码
public:
    QList<ChatWidget*> m_chatList;//保存聊天窗口
    QList<GroupWidget*> m_groupList;//保存聊天窗口
};

#endif // CHATSOCKET_H
